package introse;
import com.sun.net.ssl.internal.ssl.Debug;
import java.sql.*;

public class databaseConn {
    Connection conn;
    
    private boolean connect(){
        try{
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sashmirgasul", "sashmirgasul", "sashmirgasul"); 
        }
        catch(SQLException ex){
            Debug.println("%s", ex.getMessage());
            //Error Message
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("Could not connect to the database");
            errorMessage.setVisible(true);
            return false;
        }
        return true;
    }
    
    //Excecute SQL statements
    public boolean sqlStatement(String sqlStatement){
        try{
            if(connect()){
                Statement stmt = conn.createStatement();
                stmt.executeUpdate(sqlStatement);
                conn.close();
                return true;
            }
            else{
                return false;
            }
        }
        catch(SQLException ex){
            Debug.println("sqlStatement", ex.getMessage());
            return false;
        }
    }
    
    //Excecute SELECT statements
    public ResultSet sqlRead(String sqlStatement){
        try{
            connect();
            Statement stmt = conn.createStatement();
            ResultSet result = stmt.executeQuery(sqlStatement);
            return result;
        }
        catch(SQLException ex){
            Debug.println("SqlRead", ex.getMessage());
            return null;
        }
    }
}
